package Tool;

public class CheckSum {
    /** 计算校验位 ，返回十六进制校验位 */
    public static String makeCheckSum(String data) {
        data = data.replaceAll(" ","");
        int dSum = 0;
        int length = data.length();
        int index = 0;
        // 遍历十六进制，并计算总和
        while (index < length) {
            String s = data.substring(index, index + 2); // 截取2位字符
            dSum += Integer.parseInt(s, 16); // 十六进制转成十进制 , 并计算十进制的总和
            index = index + 2;
        }

        int mod = dSum % 256; // 用256取余，十六进制最大是FF，FF的十进制是255
        String checkSumHex = Integer.toHexString(mod); // 余数转成十六进制
        length = checkSumHex.length();
        if (length < 2) {
            checkSumHex = "0" + checkSumHex;  // 校验位不足两位的，在前面补0
        }
        return checkSumHex;
    }

    public static void main(String[] args) {
        String ss = "0C 00 00 00 00 00 00 01 5D 03 00 28";
        System.out.println(makeCheckSum(ss));

    }
}
